package com.jic.product.impl;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.constant.consist.ResponseCode;
import com.jic.product.entity.ProductCalibration;
import com.jic.product.exception.AppRuntimeException;
import com.jic.product.manage.request.ProductCalibrationRequest;
import com.jic.product.manage.response.ProductCalibrationResponse;
import com.jic.product.manage.response.ProductResponse;
import com.jic.product.mapper.ProductCalibrationMapper;
import com.jic.product.service.ProductCalibrationService;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 商品校标表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-05-08 02:52:23
 */
@Service
@Slf4j
public class ProductCalibrationServiceImpl implements ProductCalibrationService {

  @Autowired
  private ProductCalibrationMapper productCalibrationMapper;

  @Override
  public RestResult<String> insert(ProductCalibrationRequest request) {
    try {
      ProductCalibration productCalibration = new ProductCalibration();
      BeanUtils.copyProperties(request, productCalibration);
      productCalibration.setProductParentId(request.getProductParentId());
      for (int i = 0; i < request.getProductIds().size(); i++) {
        productCalibration.setProductId(request.getProductIds().get(i));
        Integer queryCount = productCalibrationMapper.queryCount(productCalibration);
        if (queryCount > 0) {
          continue;
        }
        productCalibrationMapper.insert(productCalibration);
      }
    } catch (BeansException e) {
      log.error("保存校标商品异常", e);
      throw new AppRuntimeException(ResponseCode.EXCEPTION);
    }
    return RestResult.success("保存校标商品成功");
  }

  @Override
  public RestResult<String> delete(ProductCalibration productCalibration) {
    long count = productCalibrationMapper.delete(productCalibration);
    if (count < 1) {
      return RestResult.success("error");
    }
    return RestResult.success("success");
  }

  @Override
  public RestResult<String> update(ProductCalibration productCalibration) {
    long count = productCalibrationMapper.update(productCalibration);
    if (count < 1) {
      return RestResult.success("error");
    }
    return RestResult.success("success");
  }

  @Override
  public RestResult<ProductCalibrationResponse> queryByPrimaryKey(ProductCalibration productCalibration) {
    return RestResult.success(null);
  }

  @Override
  public RestResult<List<ProductCalibrationResponse>> query(ProductCalibration productCalibration) {
    return RestResult.success(productCalibrationMapper.query(productCalibration));
  }

  @Override
  public RestResult<PageResult<ProductResponse>> queryPage(ProductCalibrationRequest request, Page pageQuery) {
    //计算下标
    int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
    List<ProductResponse> productResponses = productCalibrationMapper.queryPage(startIndex, pageQuery.getLimit(), request);
    long count = productCalibrationMapper.queryPageCount(request);
    PageResult<ProductResponse> pageResult = new PageResult<>();
    pageResult.setRows(productResponses);
    pageResult.setTotal(count);
    return RestResult.success(pageResult);
  }
}